package com.kaikeba.wx.filter;


import com.kaikeba.bean.Courier;
import com.kaikeba.bean.User;
import com.kaikeba.util.UserUtil;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-08-03 21:09
 * @Modified By:
 */
@WebFilter("/index.html")
public class UserFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)servletRequest;
        HttpServletResponse resp = (HttpServletResponse)servletResponse;
        /*
        * 思路： 先查看是否是快递员，再查看是否是普通用户
        * */
        Courier courier = UserUtil.getWxCourier(req.getSession());
        if (courier==null){
            //查看是否是普通用户
            User user = UserUtil.getWxUser(req.getSession());
            if (user!=null){
                //是普通用户
                filterChain.doFilter(servletRequest,servletResponse);
            }else{
                //没有这个数据，跳转到登录页面
                resp.sendRedirect("login.html");
            }
        }else{
            //是快递员
            filterChain.doFilter(servletRequest,servletResponse);
        }

    }

    @Override
    public void destroy() {

    }
}
